const mongoose = require('mongoose')

const { Schema, model } = mongoose

// User的某些字段都是关联着话题的 所以它们存的是mongoose的objectId

const userSchema = new Schema({
  __v: { type: Number, select: false },
  name: { type: String, required: true },
  password: { type: String, required: true, select: false },
  avatar_url: { type: String },
  gender: { type: String, enum: ['male', 'female'], default: 'male', required: true },
  // 简介标题
  headline: { type: String },
  // 事业
  business: { type: Schema.Types.ObjectId, ref: 'Topic', select: false },
  // 工作经历
  employments: {
    type: [{
      company: { type: Schema.Types.ObjectId, ref: 'Topic', select: false },
      job: { type: Schema.Types.ObjectId, ref: 'Topic', select: false },
    }],
    select: false,
  },

  // 教育经历
  educations: {
    type: [{
      school: { type: Schema.Types.ObjectId, ref: 'Topic', select: false },
      major: { type: Schema.Types.ObjectId, ref: 'Topic', select: false },
      diploma: { type: Number, enum: [1, 2, 3, 4, 5] }, // 学历 1:高中以下
      entrance_year: { type: Number },
      graduation_year: { type: Number },
    }],
    select: false,
  },
  // 用户关注的人 (多对多)
  following: {
    type: [
      // 这样声明可以使用populate 非常快的查到用户信息
      { type: Schema.Types.ObjectId, ref: 'User' }],
    select: false
  },
  // 用户关注的话题 (多对多)
  followingTopics: {
    type: [{ type: Schema.Types.ObjectId, ref: 'Topic' }],
    select: false,
  },
  // 用户赞过的答案列表
  likingAnswers: {
    type: [{ type: Schema.Types.ObjectId, ref: 'Answer' }],
    select: false
  },
  // 用户踩过的答案列表
  dislikingAnswers: {
    type: [{ type: Schema.Types.ObjectId, ref: 'Answer' }],
    select: false
  },
  // 用户收藏的答案列表
  collectingAnswers: {
    type: [{ type: Schema.Types.ObjectId, ref: 'Answer' }],
    select: false,
  },
})

module.exports = model('User', userSchema)